<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/4/29
 * Time: 17:30
 */

namespace app\admin\controller\qzworks;

use app\common\controller\Backend;
use think\Db;

//use think\Db\Where;
//use think\Exception;

class Qzworks extends Backend
{
    public function _initialize()
    {
        parent::_initialize();
    }

    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            $map['a.isdelete'] = ["eq", 1];
            //$where['title'] = ['like', '%php%'];
            $map['a.pub_area'] = ["eq", 2];
            $map['a.fb_status'] = ["eq", 1];
            if ($this->request->param('nickname')) {
                $id = Db::name('qz_user')->where('nickname', 'like', $this->request->param('nickname') . "%")
                    ->column('id');
                //dump($id);die;
                $map['a.uid'] = ["in", $id];
            }
            if ($this->request->param('title')) {
                $map['a.title'] = ['like', $this->request->param('title') . "%"];
            }
            if ($this->request->param('type') != "") {
                $map['a.type'] = ["eq", $this->request->param('type')];
            }


            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
//            if($this->request->param('sort')=="age"){
//                $sort="birthday";
//            }
            //dump($map);die;
            list($map, $sort, $order, $offset, $limit) = $this->buildparams();
            //发布区域判断属于活动还是文章，用于实现多表格切换


            $list = Db::name('qz_works')->alias('a')
                ->join('qz_user', 'a.uid=qz_user.userid')
                ->field('a.id,a.title,a.praise_points,a.collection,a.releast_time,a.pub_area,a.type,a.content,
                                                a.fb_status,a.status,a.card_num,a.status,a.watch_num,a.comment_num,nickname,img,userid,a.mus_or_video')
                ->where($map)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();
            //dump(Db::table('contract')->getLastSql());die;
            //获取js需要的数据
//            foreach ($list as $k => $v) {
//                $list[$k]['releast_time'] = date('Y:m:d H:i:s',$list[$k]['releast_time']);
//            }

            //获取js需要的数据
            foreach ($list as $k => $v) {
                $list[$k]['releast_time'] = date('Y:m:d H:i:s', $list[$k]['releast_time']);

                $list[$k]['mus_or_video'] = json_decode($list[$k]['mus_or_video'], true);
            }

            $count = Db::name('qz_works')
                ->where($map)
                ->count();

//            $this->view->assign('list', $list);
            $result = array("total" => $count, "rows" => $list);
            //echo 2222;exit;
            return json($result);
        }
        return $this->view->fetch();
    }

    //修改点赞量
    public function edit_dzl()
    {
        $post = $this->request->param();
        //print_r($data);die;
        if (!empty($post['id']) && !empty($post['praise_points'])) {
            $data = array(
                'praise_points' => $post['praise_points']
            );
            $save = Db::name('qz_works')->where('id', $post['id'])
                ->update($data);
            //print_r($save);die;
            if ($save) {
                return 1;
                //return ajax_return_adv('修改成功');
                //return ajax_return('','ok',0,'');

                //return layer.msg(data.msg);
                //return ajax_return_adv("ok");
            } else {
                return 0;
                //return ajax_return_adv_error("修改失败");
            }
        }

    }

    //修改收藏量
    public function edit_scl()
    {
        $post = $this->request->param();
        //print_r($data);die;
        if (!empty($post['id']) && !empty($post['collection'])) {
            $data = array(
                'collection' => $post['collection']
            );
            $save = Db::name('qz_works')->where('id', $post['id'])
                ->update($data);
            //print_r($save);die;
            if ($save) {
                return 1;
                // return ajax_return_adv('修改成功');
                //return ajax_return('','ok',0,'');
            } else {
                return 0;
                //return ajax_return_adv_error("修改失败");
            }
        }
    }

    //修改评论量
    public function edit_pll()
    {
        $post = $this->request->param();
        //print_r($data);die;
        if (!empty($post['id']) && !empty($post['comment_num'])) {
            $data = array(
                'comment_num' => $post['comment_num']
            );
            $save = Db::name('qz_works')->where('id', $post['id'])
                ->update($data);
            //print_r($save);die;
            if ($save) {
                return 1;
                // return ajax_return_adv('修改成功');
                //return ajax_return('','ok',0,'');
            } else {
                return 0;
                //return ajax_return_adv_error("修改失败");
            }
        }
    }

    //修改播放量
    public function edit_bfl()
    {
        $post = $this->request->param();
        //print_r($data);die;
        if (!empty($post['id']) && !empty($post['watch_num'])) {
            $data = array(
                'watch_num' => $post['watch_num']
            );
            $save = Db::name('qz_works')->where('id', $post['id'])
                ->update($data);
            //print_r($save);die;
            if ($save) {
                return 1;
                // return ajax_return_adv('修改成功');
                //return ajax_return('','ok',0,'');
            } else {
                return 0;
                //return ajax_return_adv_error("修改失败");
            }
        }
    }

    //文章审核
    public function edit_sh()
    {
        $param = $this->request->param();
        if($param['ids'] && $param['editstate'] ){
            $res=Db::name('qz_works')->where('id',$param['ids'])->update(['status'=>$param['editstate']]);
            if($res){
                $this->success('审核成功');
            }else{
                $this->success('审核失败');
            }
        }
    }

    /**
     * 默认恢复操作
     */
    public function resume()
    {
        //return $this->updateField($this->fieldStatus, 1, "恢复成功");
        $post = $this->request->param();
        if (!empty($post['id']) && !empty($post['zt'])) {
            //zt为ok时，审核通过，no时驳回
            if ($post['zt'] == "ok") {
                $status = 2;
            } else if ($post['zt'] == "no") {
                $status = 0;
            }
            $res = Db::name('qz_works')->where('id', $post['id'])
                ->update(['status' => $status]);
            if ($res) {
                return ajax_return_adv("ok", '');
                //return ajax_return($data, $msg, $code, $extend);
            }
        }
    }

    /**
     * 查看文章详情
     */
    public function  detail(){

        $id = $this->request->param('ids');
        $id = (int)$id;

        $detail = Db::name('qz_works')
            ->alias('w')
            ->join('qz_user u','u.userid =w.uid ')
            ->field('u.userid,u.nickname,u.img,w.title,w.type,w.status,w.mus_or_video,w.content,w.praise_points,w.releast_time,w.collection,w.watch_num,w.comment_num')
            ->where("w.id",$id)
            ->find();

        $detail['mus_or_video'] = json_decode($detail['mus_or_video'],true);
        //dump($detail);die;
        $this->assign('row',$detail);
        return $this->fetch();
    }


    /**
     *   批量修改状态
     * @param   string $status 要修改的状态    exp: 0/2    "0": 不通过 | "2":通过
     * @param   array $ids id数组          exp: [1,2,3,....]
     */
    public function manyUpdateStatus()
    {
        $ids = $this->request->request('ids/a');
        //dump($ids);die;
        $status = $this->request->post('status');
        if ($status != '0' && $status != '2') {
            $this->error("非法操作");
        }
        if (is_array($ids) && $ids) {
            Db::name('qz_works')->where('id', 'in', $ids)->update(['status' => $status]);
            if ($status == '2') {
                $this->success("审核已通过");
            } else {
                $this->success("操作成功,未通过");
            }
        } else {
            $this->error("数据格式有误");
        }

    }


    /**
     * 软删除
     */
    public function delete()
    {
        $id = trim($this->request->param('ids'));
        if ($id) {
            $res = Db::name('qz_works')->where('id', $id)->update(['is_delete' => 0]);
            if ($res) {
                $this->success('操作成功');
            } else {
                $this->error('操作失败');
            }
        }
    }
    /**
     * 手动设置中奖人员名单
     */
    public function lottery()
    {
        $param=$this->request->param();
        //获取传递过来的活动id和用户的userid
        if($param['w_id'] && $param['userid']){
            $data['zj_time']=time();
            $data['static']=1;
            $res=Db::name('qz_use_activity')->where(array('wid'=>$param['w_id'],'uid'=>$param['userid']))->update($data);
            if($res){
                $this->success('操作成功');
            }else{
                $this->error('操作失败');
            }
        }
    }
}